﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Mengsk.ProxyDelivery.GoodsSpider
{
    [Serializable]
    class SystemConfig
    {
        private static SystemConfig instance = null;
        public static readonly string SYSTEM_CONFIG_PATH = App.DIR_CONFIG + typeof(SystemConfig).FullName + ".cfg";

        public static SystemConfig Instance { get { return instance; } }

        public string WeightDeviceType { get; set; }

        public string WebServiceUrl { get; set; }

        public static void Load()
        {
            if (File.Exists(SYSTEM_CONFIG_PATH))
            {
                using (FileStream fs = new FileStream(SYSTEM_CONFIG_PATH, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    instance = bf.Deserialize(fs) as SystemConfig;
                }
            }
            else
            {
                instance = new SystemConfig();
            }
        }

        public void Save()
        {
            using (FileStream fs = new FileStream(SYSTEM_CONFIG_PATH, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, instance);
            }
        }
    }
}
